package com.zheng.common.web.tag;

import com.github.pagehelper.util.StringUtil;
import com.zheng.common.util.SpringContextUtil;
import com.zheng.upms.rpc.api.UpmsDictService;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * 将数据字典通过typeKey 转换成value
 */
public class DataDictValueTag extends TagSupport {

    private String typeCode;
    private String typeKey;

    public int doStartTag() throws JspTagException {
        return EVAL_PAGE;
    }

    public int doEndTag() throws JspTagException {
        try {
            JspWriter out = this.pageContext.getOut();
            out.print(dictName().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    private StringBuffer dictName() {

        StringBuffer sb = new StringBuffer();
        if (StringUtil.isNotEmpty(this.getTypeCode()) && StringUtil.isNotEmpty(this.getTypeKey()))
        {

            UpmsDictService upmsDictService = (UpmsDictService) SpringContextUtil.getBean("upmsDictService");
            String keyValue = upmsDictService.queryDataDictTextByValue(this.getTypeCode(), this.getTypeKey());
            if(StringUtil.isEmpty(keyValue))
                sb.append("字典未配置");
            else{
                sb.append(keyValue);
            }
        }
        return sb;
    }

    public String getTypeCode() {
        return typeCode;
    }

    public void setTypeCode(String typeCode) {
        this.typeCode = typeCode;
    }

    public String getTypeKey() {
        return typeKey;
    }

    public void setTypeKey(String typeKey) {
        this.typeKey = typeKey;
    }
}